package com.zhiyou100.review.day020.homework;

/**
 * @packageName: SellingTickets
 * @className: com.zhiyou100.review.day020.text
 * @Description: TODO 卖票 四个窗口卖一辆火车的100张票  实现线程同步
 * @author: yang
 * @date: 7/1/20
 */
public class Homework02 {
    public static void main(String[] args) {
        MyTickets myTickets = new MyTickets();
        Thread window1 = new Thread(myTickets, "窗口 1");
        Thread window2 = new Thread(myTickets, "窗口 2");
        Thread window3 = new Thread(myTickets, "窗口 3");

        window1.start();
        window2.start();
        window3.start();


    }
}

class RailwayTicket {
    static Integer number = 100;
}

class MyTickets implements Runnable {

    @Override
    public void run() {
        for (; ; ) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (RailwayTicket.number) {

                if (RailwayTicket.number > 0) {
                    // 票数大于0 继续卖
                    RailwayTicket.number--;
                    // 票数 --
                    System.out.println(Thread.currentThread().getName() + " 正在卖 第 " + (100 - RailwayTicket.number) + " 票");
                } else {
                    // 卖完 跳出
                    System.out.println(Thread.currentThread().getName() + " 票卖光了 ！！！！！！");
                    break;
                }
            }
        }
    }
}
